package com.example.pmanagesys.controller;
// 日志管理

import com.example.pmanagesys.entity.Log;
import com.example.pmanagesys.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/log")
public class LogControlly {

    //  行为日志 页面
    @GetMapping("/file")
    public ModelAndView home() {
        return new ModelAndView("Log");
    }

    @Autowired
    private LogMapper logMapper;

    // 新增
    @PostMapping
    public Integer save(@RequestBody Log log) {
        return logMapper.save(log);
    }

    // 分页查询  localhost:8080/log/page?pageNum=1&pageSize=10&operator=&logType=&requestInterface=
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                        @RequestParam String operator, @RequestParam String logType,
                                        @RequestParam String requestInterface)  {
        // 模糊查询的参数 (可以为空）
        operator = "%"+operator+"%";
        logType = "%"+logType+"%";
        requestInterface = "%"+requestInterface+"%";
        // 某一页的数据
        pageNum = (pageNum - 1) * pageSize;
        List<Log> data = logMapper.selectPage(pageNum, pageSize,operator,logType,requestInterface);
        // 总条数
        Integer total = logMapper.selectTotal(operator,logType,requestInterface);
        // 封装一个map装数据
        Map<String, Object> res = new HashMap<>();
        res.put("logData", data);
        res.put("total",total);
        return res;
    }

}